<?php
namespace Admin\Controller;
use Admin\Controller;

class SlideController extends BaseController
{

    public function index($key="")
    {
        if($key === ""){
            $model = M('slide');  
        }else{
            $where['title'] = array('like',"%$key%");
            $where['id'] = array('like',"%$key%");
            $where['_logic'] = 'or';
            $model = M('slide')->where($where); 
        } 
        
        $count  = $model->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Extend\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $pages = $model->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('id DESC')->select();
        $this->assign('model', $pages);
        $this->assign('page',$show);
        $this->display();     
    }


    //幻灯片添加方法
    public function add()
    {
        if (!IS_POST) { //没有post动作显示默认模板页面
		    $this->assign("category",getSortedCategory(M('category')->select()));
            $this->display();
        }
        if (IS_POST) {
           $model = D("Slide");
		   if(!empty($_FILES)){  //判断是否有文件上传
		   $config = array(
		   'rootPath'      =>  './Public/', //保存根路径
		   'savePath'      =>  'slide/', //保存路径
		   );
			$upload = new \Think\Upload($config); //有图片上传实例化upload，把文件上传指定目录
            $cg = $upload -> upload(array($_FILES['pic']));//调用upload函数并将参数$_FILES数组传进去
			if($cg){
			//循环输出拼装好的图片路径给$bigimg
			foreach($cg as $vo)
			{
			$bigimg.=$vo['savepath'].$vo['savename']."|";
			$_POST['imgurl'] = $bigimg;
			}
			}
		    }
          
            $model->time = time();
            $model->user_id = 1;
            if (!$model->create()) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($model->getError());
                exit();
            } else {
                if ($model->add()) {
                $this->success("添加成功", U('Slide/index'));
                } else {
                $this->error("添加失败");
                }
            }
        }
    }

    public function update($id)
    {
    	$id = intval($id);
        if (!IS_POST) {
            $model = M('slide')->where("id= %d",$id)->find();
            $this->assign("category",getSortedCategory(M('category')->select()));
            $this->assign('post',$model);
            $this->display();
        }
        if (IS_POST) {
           $model = D("Slide");
		   if(!empty($_FILES)){  //判断是否有文件上传
		   $config = array(
		   'rootPath'      =>  './Public/', //保存根路径
		   'savePath'      =>  'slide/', //保存路径
		   );
		   $upload = new \Think\Upload($config); //有图片上传实例化upload，把文件上传指定目录
           $cg = $upload -> upload(array($_FILES['pic']));//调用upload函数并将参数$_FILES数组传进去
		   if($cg){
			//循环输出拼装好的图片路径给$bigimg
			foreach($cg as $vo)
			{
			$bigimg.=$vo['savepath'].$vo['savename']."|";
			$_POST['imgurl'] = $bigimg;
			}
			}
		    }
            if (!$model->create()) {
                $this->error($model->getError());
            }else{
			       $model->id = $id;
                   $data= M('Slide')->field("imgurl")->where("slide.id=%d",$id)->find();
                    //var_dump($data);
			        $model->imgurl=$data['imgurl'].$bigimg;
                if ($model->save()) {
                    $this->success("更新成功", U('Slide/index'));
                } else {
                    $this->error("更新失败");
                }        
            }
        }
    }


    public function delete($id)
    {
    	$id = intval($id);
        $model = M('slide');
        $result = $model->where("id= %d",$id)->delete();
        if($result){
            $this->success("删除成功", U('Slide/index'));
        }else{
            $this->error("删除失败");
        }
    }
	
         //删除数组指定值	
         public function delarr_1($id){
             header("Content-Type:text/html; charset=utf-8");	
             $tmp = $_POST['url'];
             $data= M('slide')->field("imgurl")->where("id=%d",$id)->find();
             $datav=$data['imgurl'];
             $arr = explode("|",$datav);
             foreach( $arr as $k=>$v) {
                if($tmp == $v) unset($arr[$k]);
             }
             $char = implode("|", $arr);
             //echo $char;
             $house = M("slide");
             $data['imgurl'] = $char;
             $condition['id'] = $_POST['id'];
             $result = $house ->where($condition)->save($data);
             if($result !== false){
                 echo '数据更新成功！';
             }else{
             echo '数据更新失败！';
             }
             }
	
	
	public function push($id) {
		$id = intval($id);
		if (IS_GET) {
			$status = M('slide') -> where("id= %d",$id) -> getField('status');
			if ($status === '0') {
				$data['status'] = 1;
			} else {
				$data['status'] = 0;
			}
			$result = M('slide') -> where("id= %d",$id) -> save($data);
			if ($result && $data['status'] === 1) {
				$this -> success("发布成功", U('Slide/index'));
			} elseif ($result && $data['status'] === 0) {
				$this -> success("撤销成功", U('Slide/index'));
			} else {
				$this -> error("操作失败");
			}
		} else {
			exit;

		}
	}




}
